package parser

import (
	"fmt"
	"regexp"

	"aqielife.cn/spider/singleSpider/zhenai/model"
)

const cityListRe = `<a href="(http://www.zhenai.com/zhenghun/[\w]+)"[^>]*>([^<]+)</a>`

// $('#cityList>dd>a')

// 下一页
var cityNextUrlReg = regexp.MustCompile(`<li[^>]*class="paging-item"><a[^>]*href="(http://www.zhenai.com/zhenghun/[^"]+)">下一页</a>`)

// http://www.zhenai.com/zhenghun 城市列表
func ParseCityList(contents []byte) model.ParseResult {
	re := regexp.MustCompile(cityListRe)
	matches := re.FindAllSubmatch(contents, -1)
	// 打印爬取总数
	fmt.Printf("total Number %d \n", len(matches))

	result := model.ParseResult{}
	// 限制十个城市
	limit := 1
	for _, m := range matches {

		result.Items = append(result.Items, "City "+string(m[:][2]))
		result.Requests = append(result.Requests, model.Request{
			Url:        string(m[:][1]),
			ParserFunc: ParseCity,
		})
		limit--
		if limit == 0 {
			break
		}
	}
	//下一页的Request
	/*matches = cityNextUrlReg.FindAllStringSubmatch(string(contents), -1)
	for _, m := range matches {
		result.Request = append(result.Request, engine.Request{
			Url:    m[1],
			Parser: engine.NewFuncParser(ParseCity, config.ParseCity),
		})
	}*/
	return result
}
